<?php


namespace App\Admin\Controllers\ApiDoc;


use App\Constant\ParamType;
use App\Constant\RequestPart;
use App\Http\Controllers\Controller;
use App\Model\Repository\ApiRepository;
use App\Model\Repository\ApiRequestBodyRepository;
use Illuminate\Http\Request;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ApiRequestBodyController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('Index');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Show interface.
     *
     * @param $id
     * @return Content
     */
    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('Detail');
            $content->description('description');

            $content->body(Admin::show(ApiRequestBodyRepository::findOrFail($id), function (Show $show) {
                $show->field('param_id', '参数id');
                $show->field('param_name', '参数名称');
                $show->field('param_type', '参数类型');
                $show->field('default', '默认值');
                $show->api('所属接口', function (Show $show) {
                    $show->field('api_name', '接口名称');
                });
                $show->field('remark', '备注');

                $show->field('created_at', '创建时间');
                $show->field('updated_at', '修改时间');
            }));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('Edit');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('创建');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(ApiRequestBodyRepository::class, function (Grid $grid) {

            $grid->column('param_id', '参数id')->sortable();
            $grid->column('param_name', '参数名称');
            $grid->column('param_type', '参数类型');
            $grid->column('part', '提交位置');
            $grid->column('api.api_name', '所属接口');
            $grid->column('required', '是否必填')->switch([
                'on' => ['value' => '0', 'text' => '选填', 'color' => 'default'],
                'off' => ['value' => '1', 'text' => '必填', 'color' => 'primary'],
            ]);
            $grid->column('created_at', '创建时间');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->equal('api_id','所属接口')->select(ApiRepository::getApiMap());
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(ApiRequestBodyRepository::class, function (Form $form) {

            $form->display('param_id', '参数id');
            $form->text('param_name', '参数名称')->rules('required');
            $form->text('default', '默认值')->default("0");
            $form->text('remark', '备注')->default('暂无');

            $form->switch('required', '是否必填')->states([
                'on' => ['value' => '0', 'text' => '选填', 'color' => 'default'],
                'off' => ['value' => '1', 'text' => '必填', 'color' => 'primary'],
            ]);
            $form->select('api_id', '所属接口')->options(ApiRepository::getApiMap())->rules('required');
            $form->select('param_type', '参数类型')->rules('required')->options(ParamType::getSelectOptions());
            $form->select('part', '提交位置')->options(RequestPart::getSelectOptions())->default('body');


            $form->display('created_at', '创建时间');
            $form->display('updated_at', '修改时间');
        });
    }
}